home *** CD-ROM | disk | FTP | other *** search
/ Amiga Games Extra 1996 September / Amiga Games Extra CD-ROM 9-1996.iso / userbox / publicdomain / vim-4.2 / src / ops.c < prev    next >
C/C++ Source or Header  |  1996-06-12  |  62KB  |  2,673 lines

  1. /* vi:set ts=4 sw=4:
  2.  *
  3.  * VIM - Vi IMproved        by Bram Moolenaar
  4.  *
  5.  * Do ":help uganda"  in Vim to read copying and usage conditions.
  6.  * Do ":help credits" in Vim to see a list of people who contributed.
  7.  */
  8.  
  9. /*
  10.  * ops.c: implementation of various operators: do_shift, do_delete, do_tilde,
  11.  *          do_change, do_yank, do_put, do_join
  12.  */
  13.  
  14. #include "vim.h"
  15. #include "globals.h"
  16. #include "proto.h"
  17. #include "option.h"
  18. #include "ops.h"
  19.  
  20. /*
  21.  * Number of registers.
  22.  *      0 = unnamed register, for normal yanks and puts
  23.  *   1..9 = number registers, for deletes
  24.  * 10..35 = named registers
  25.  *     36 = delete register (-)
  26.  *     37 = GUI selection register (*). Only if USE_GUI defined
  27.  */
  28. #ifdef USE_GUI
  29. # define NUM_REGISTERS            38
  30. #else
  31. # define NUM_REGISTERS            37
  32. #endif
  33.  
  34. /*
  35.  * Symbolic names for some registers.
  36.  */
  37. #define DELETION_REGISTER        36
  38. #ifdef USE_GUI
  39. # define GUI_SELECTION_REGISTER    37
  40. #endif
  41.  
  42. /*
  43.  * Each yank buffer is an array of pointers to lines.
  44.  */
  45. static struct yankbuf
  46. {
  47.     char_u        **y_array;        /* pointer to array of line pointers */
  48.     linenr_t     y_size;         /* number of lines in y_array */
  49.     char_u        y_type;         /* MLINE, MCHAR or MBLOCK */
  50. } y_buf[NUM_REGISTERS];
  51.  
  52. static struct    yankbuf *y_current;            /* ptr to current yank buffer */
  53. static int        yankappend;                    /* TRUE when appending */
  54. static struct    yankbuf *y_previous = NULL; /* ptr to last written yank buffr */
  55.  
  56. /*
  57.  * structure used by block_prep, do_delete and do_yank for blockwise operators
  58.  */
  59. struct block_def
  60. {
  61.     int            startspaces;
  62.     int            endspaces;
  63.     int            textlen;
  64.     char_u        *textstart;
  65.     colnr_t        textcol;
  66. };
  67.  
  68. static void        get_yank_buffer __ARGS((int));
  69. static int        stuff_yank __ARGS((int, char_u *));
  70. static void        free_yank __ARGS((long));
  71. static void        free_yank_all __ARGS((void));
  72. static void        block_prep __ARGS((struct block_def *, linenr_t, int));
  73. static int        same_leader __ARGS((int, char_u *, int, char_u *));
  74. static int        fmt_end_block __ARGS((linenr_t, int *, char_u **));
  75.  
  76. /*
  77.  * do_shift - handle a shift operation
  78.  */
  79.     void
  80. do_shift(op, curs_top, amount)
  81.     int             op;
  82.     int                curs_top;
  83.     int                amount;
  84. {
  85.     register long    i;
  86.     int                first_char;
  87.  
  88.     if (u_save((linenr_t)(curwin->w_cursor.lnum - 1),
  89.                    (linenr_t)(curwin->w_cursor.lnum + op_line_count)) == FAIL)
  90.         return;
  91.     for (i = op_line_count; --i >= 0; )
  92.     {
  93.         first_char = *ml_get_curline();
  94.         if (first_char == NUL)                            /* empty line */
  95.             curwin->w_cursor.col = 0;
  96.         /*
  97.          * Don't move the line right if it starts with # and p_si is set.
  98.          */
  99.         else
  100. #if defined(SMARTINDENT) || defined(CINDENT)
  101.             if (first_char != '#' || (
  102. # ifdef SMARTINDENT
  103.                          !curbuf->b_p_si
  104. # endif
  105. # if defined(SMARTINDENT) && defined(CINDENT)
  106.                              &&
  107. # endif
  108. # ifdef CINDENT
  109.                          (!curbuf->b_p_cin || !in_cinkeys('#', ' ', TRUE))
  110. # endif
  111.                                         ))
  112. #endif
  113.         {
  114.             /* if (op_block_mode)
  115.                     shift the block, not the whole line
  116.             else */
  117.                 shift_line(op == LSHIFT, p_sr, amount);
  118.         }
  119.         ++curwin->w_cursor.lnum;
  120.     }
  121.  
  122.     if (curs_top)            /* put cursor on first line, for ">>" */
  123.     {
  124.         curwin->w_cursor.lnum -= op_line_count;
  125.         beginline(MAYBE);    /* shift_line() may have changed cursor.col */
  126.     }
  127.     else
  128.         --curwin->w_cursor.lnum;        /* put cursor on last line, for ":>" */
  129.     updateScreen(CURSUPD);
  130.  
  131.     if (op_line_count > p_report)
  132.        smsg((char_u *)"%ld line%s %ced %d time%s", op_line_count,
  133.                            plural(op_line_count), (op == RSHIFT) ? '>' : '<',
  134.                            amount, plural((long)amount));
  135. }
  136.  
  137. /*
  138.  * shift the current line one shiftwidth left (if left != 0) or right
  139.  * leaves cursor on first blank in the line
  140.  */
  141.     void
  142. shift_line(left, round, amount)
  143.     int left;
  144.     int    round;
  145.     int    amount;
  146. {
  147.     register int count;
  148.     register int i, j;
  149.     int p_sw = (int)curbuf->b_p_sw;
  150.  
  151.     count = get_indent();            /* get current indent */
  152.  
  153.     if (round)                        /* round off indent */
  154.     {
  155.         i = count / p_sw;            /* number of p_sw rounded down */
  156.         j = count % p_sw;            /* extra spaces */
  157.         if (j && left)                /* first remove extra spaces */
  158.             --amount;
  159.         if (left)
  160.         {
  161.             i -= amount;
  162.             if (i < 0)
  163.                 i = 0;
  164.         }
  165.         else
  166.             i += amount;
  167.         count = i * p_sw;
  168.     }
  169.     else                /* original vi indent */
  170.     {
  171.         if (left)
  172.         {
  173.             count -= p_sw * amount;
  174.             if (count < 0)
  175.                 count = 0;
  176.         }
  177.         else
  178.             count += p_sw * amount;
  179.     }
  180.     set_indent(count, TRUE);        /* set new indent */
  181. }
  182.  
  183. #if defined(LISPINDENT) || defined(CINDENT)
  184. /*
  185.  * do_reindent - handle reindenting a block of lines for C or lisp.
  186.  *
  187.  * mechanism copied from do_shift, above
  188.  */
  189.     void
  190. do_reindent(how)
  191.     int (*how) __ARGS((void));
  192. {
  193.     register long   i;
  194.     char_u            *l;
  195.     int                count;
  196.  
  197.     if (u_save((linenr_t)(curwin->w_cursor.lnum - 1),
  198.                     (linenr_t)(curwin->w_cursor.lnum + op_line_count)) == FAIL)
  199.         return;
  200.  
  201.     for (i = op_line_count; --i >= 0 && !got_int; )
  202.     {
  203.         /* it's a slow thing to do, so give feedback so there's no worry that
  204.          * the computer's just hung. */
  205.  
  206.         if ((i % 50 == 0 || i == op_line_count - 1) && op_line_count > p_report)
  207.             smsg((char_u *)"%ld line%s to indent... ", i, plural(i));
  208.  
  209.         /*
  210.          * Be vi-compatible: For lisp indenting the first line is not
  211.          * indented, unless there is only one line.
  212.          */
  213. #ifdef LISPINDENT
  214.         if (i != op_line_count - 1 || op_line_count == 1 ||
  215.                                                        how != get_lisp_indent)
  216. #endif
  217.         {
  218.             l = skipwhite(ml_get_curline());
  219.             if (*l == NUL)                    /* empty or blank line */
  220.                 count = 0;
  221.             else
  222.                 count = how();                /* get the indent for this line */
  223.             
  224.             set_indent(count, TRUE);
  225.         }
  226.         ++curwin->w_cursor.lnum;
  227.     }
  228.  
  229.     /* put cursor on first non-blank of indented line */
  230.     curwin->w_cursor.lnum -= op_line_count;
  231.     beginline(MAYBE);
  232.  
  233.     updateScreen(CURSUPD);
  234.  
  235.     if (op_line_count > p_report)
  236.     {
  237.         i = op_line_count - (i + 1);
  238.         smsg((char_u *)"%ld line%s indented ", i, plural(i));
  239.     }
  240. }
  241. #endif /* defined(LISPINDENT) || defined(CINDENT) */
  242.  
  243. /*
  244.  * check if character is name of yank buffer
  245.  * Note: There is no check for 0 (default register), caller should do this
  246.  */
  247.      int
  248. is_yank_buffer(c, writing)
  249.     int        c;
  250.     int        writing;        /* if TRUE check for writable buffers */
  251. {
  252.     if (c > '~')
  253.         return FALSE;
  254.     if (isalnum(c) || (!writing && vim_strchr((char_u *)".%:", c) != NULL) ||
  255.                                                           c == '"' || c == '-'
  256. #ifdef USE_GUI
  257.                                                    || (gui.in_use && c == '*')
  258. #endif
  259.                                                         )
  260.         return TRUE;
  261.     return FALSE;
  262. }
  263.  
  264. /*
  265.  * Set y_current and yankappend, according to the value of yankbuffer.
  266.  *
  267.  * If yankbuffer is 0 and writing, use buffer 0
  268.  * If yankbuffer is 0 and reading, use previous buffer
  269.  */
  270.     static void
  271. get_yank_buffer(writing)
  272.     int        writing;
  273. {
  274.     register int i;
  275.  
  276.     yankappend = FALSE;
  277.     if (((yankbuffer == 0 && !writing) || yankbuffer == '"') &&
  278.                                                            y_previous != NULL)
  279.     {
  280.         y_current = y_previous;
  281.         return;
  282.     }
  283.     i = yankbuffer;
  284.     if (isdigit(i))
  285.         i -= '0';
  286.     else if (islower(i))
  287.         i -= 'a' - 10;
  288.     else if (isupper(i))
  289.     {
  290.         i -= 'A' - 10;
  291.         yankappend = TRUE;
  292.     }
  293.     else if (yankbuffer == '-')
  294.         i = DELETION_REGISTER;
  295. #ifdef USE_GUI
  296.     else if (gui.in_use && yankbuffer == '*')
  297.         i = GUI_SELECTION_REGISTER;
  298. #endif
  299.     else                /* not 0-9, a-z, A-Z or '-': use buffer 0 */
  300.         i = 0;
  301.     y_current = &(y_buf[i]);
  302.     if (writing)        /* remember the buffer we write into for do_put() */
  303.         y_previous = y_current;
  304. }
  305.  
  306. /*
  307.  * return TRUE if the current yank buffer has type MLINE
  308.  */
  309.     int
  310. yank_buffer_mline()
  311. {
  312.     if (yankbuffer != 0 && !is_yank_buffer(yankbuffer, FALSE))
  313.         return FALSE;
  314.     get_yank_buffer(FALSE);
  315.     return (y_current->y_type == MLINE);
  316. }
  317.  
  318. /*
  319.  * start or stop recording into a yank buffer
  320.  *
  321.  * return FAIL for failure, OK otherwise
  322.  */
  323.     int
  324. do_record(c)
  325.     int c;
  326. {
  327.     char_u        *p;
  328.     static int    bufname;
  329.     int            retval;
  330.  
  331.     if (Recording == FALSE)         /* start recording */
  332.     {
  333.                         /* registers 0-9, a-z and " are allowed */
  334.         if (c > '~' || (!isalnum(c) && c != '"'))
  335.             retval = FAIL;
  336.         else
  337.         {
  338.             Recording = TRUE;
  339.             showmode();
  340.             bufname = c;
  341.             retval = OK;
  342.         }
  343.     }
  344.     else                            /* stop recording */
  345.     {
  346.         Recording = FALSE;
  347.         MSG("");
  348.         p = get_recorded();
  349.         if (p == NULL)
  350.             retval = FAIL;
  351.         else
  352.             retval = (stuff_yank(bufname, p));
  353.     }
  354.     return retval;
  355. }
  356.  
  357. /*
  358.  * stuff string 'p' into yank buffer 'bufname' (append if uppercase)
  359.  * 'p' is assumed to be alloced.
  360.  *
  361.  * return FAIL for failure, OK otherwise
  362.  */
  363.     static int
  364. stuff_yank(bufname, p)
  365.     int bufname;
  366.     char_u *p;
  367. {
  368.     char_u *lp;
  369.     char_u **pp;
  370.  
  371.     yankbuffer = bufname;
  372.                                             /* check for read-only buffer */
  373.     if (yankbuffer != 0 && !is_yank_buffer(yankbuffer, TRUE))
  374.         return FAIL;
  375.     get_yank_buffer(TRUE);
  376.     if (yankappend && y_current->y_array != NULL)
  377.     {
  378.         pp = &(y_current->y_array[y_current->y_size - 1]);
  379.         lp = lalloc((long_u)(STRLEN(*pp) + STRLEN(p) + 1), TRUE);
  380.         if (lp == NULL)
  381.         {
  382.             vim_free(p);
  383.             return FAIL;
  384.         }
  385.         STRCPY(lp, *pp);
  386.         STRCAT(lp, p);
  387.         vim_free(p);
  388.         vim_free(*pp);
  389.         *pp = lp;
  390.     }
  391.     else
  392.     {
  393.         free_yank_all();
  394.         if ((y_current->y_array =
  395.                         (char_u **)alloc((unsigned)sizeof(char_u *))) == NULL)
  396.         {
  397.             vim_free(p);
  398.             return FAIL;
  399.         }
  400.         y_current->y_array[0] = p;
  401.         y_current->y_size = 1;
  402.         y_current->y_type = MCHAR;    /* used to be MLINE, why? */
  403.     }
  404.     return OK;
  405. }
  406.  
  407. /*
  408.  * execute a yank buffer (register): copy it into the stuff buffer
  409.  *
  410.  * return FAIL for failure, OK otherwise
  411.  */
  412.     int
  413. do_execbuf(c, colon, addcr)
  414.     int c;
  415.     int    colon;                /* insert ':' before each line */
  416.     int addcr;                /* always add '\n' to end of line */
  417. {
  418.     static int    lastc = NUL;
  419.     long        i;
  420.     char_u        *p;
  421.     int            truncated;
  422.     int            retval;
  423.  
  424.  
  425.     if (c == '@')                    /* repeat previous one */
  426.         c = lastc;
  427.     if (c == '%' || !is_yank_buffer(c, FALSE))    /* check for valid buffer */
  428.         return FAIL;
  429.     lastc = c;
  430.  
  431.     if (c == ':')                    /* use last command line */
  432.     {
  433.         if (last_cmdline == NULL)
  434.         {
  435.             EMSG(e_nolastcmd);
  436.             return FAIL;
  437.         }
  438.         vim_free(new_last_cmdline);    /* don't keep the cmdline containing @: */
  439.         new_last_cmdline = NULL;
  440.         if (ins_typebuf((char_u *)"\n", FALSE, 0, TRUE) == FAIL)
  441.             return FAIL;
  442.         if (ins_typebuf(last_cmdline, FALSE, 0, TRUE) == FAIL)
  443.             return FAIL;
  444.         if (ins_typebuf((char_u *)":", FALSE, 0, TRUE) == FAIL)
  445.             return FAIL;
  446.     }
  447.     else if (c == '.')                /* use last inserted text */
  448.     {
  449.         p = get_last_insert();
  450.         if (p == NULL)
  451.         {
  452.             EMSG(e_noinstext);
  453.             return FAIL;
  454.         }
  455.         i = STRLEN(p);
  456.         if (i > 0 && p[i - 1] == ESC)    /* remove trailing ESC */
  457.         {
  458.             p[i - 1] = NUL;
  459.             truncated = TRUE;
  460.         }
  461.         else
  462.             truncated = FALSE;
  463.         retval = ins_typebuf(p, FALSE, 0, TRUE);
  464.         if (truncated)
  465.             p[i - 1] = ESC;
  466.         return retval;
  467.     }
  468.     else
  469.     {
  470.         yankbuffer = c;
  471.         get_yank_buffer(FALSE);
  472.         if (y_current->y_array == NULL)
  473.             return FAIL;
  474.  
  475.         /*
  476.          * Insert lines into typeahead buffer, from last one to first one.
  477.          */
  478.         for (i = y_current->y_size; --i >= 0; )
  479.         {
  480.         /* insert newline between lines and after last line if type is MLINE */
  481.             if (y_current->y_type == MLINE || i < y_current->y_size - 1
  482.                                                                      || addcr)
  483.             {
  484.                 if (ins_typebuf((char_u *)"\n", FALSE, 0, TRUE) == FAIL)
  485.                     return FAIL;
  486.             }
  487.             if (ins_typebuf(y_current->y_array[i], FALSE, 0, TRUE) == FAIL)
  488.                 return FAIL;
  489.             if (colon && ins_typebuf((char_u *)":", FALSE, 0, TRUE) == FAIL)
  490.                 return FAIL;
  491.         }
  492.         Exec_reg = TRUE;        /* disable the 'q' command */
  493.     }
  494.     return OK;
  495. }
  496.  
  497. /*
  498.  * Insert a yank buffer: copy it into the Read buffer.
  499.  * Used by CTRL-R command and middle mouse button in insert mode.
  500.  *
  501.  * return FAIL for failure, OK otherwise
  502.  */
  503.     int
  504. insertbuf(c)
  505.     int c;
  506. {
  507.     long    i;
  508.     int        retval = OK;
  509.  
  510.     /*
  511.      * It is possible to get into an endless loop by having CTRL-R a in
  512.      * register a and then, in insert mode, doing CTRL-R a.
  513.      * If you hit CTRL-C, the loop will be broken here.
  514.      */
  515.     mch_breakcheck();
  516.     if (got_int)
  517.         return FAIL;
  518.  
  519.     /* check for valid buffer */
  520.     if (c != NUL && !is_yank_buffer(c, FALSE))
  521.         return FAIL;
  522.  
  523. #ifdef USE_GUI
  524.     if (c == '*')
  525.         gui_get_selection();            /* may fill * register */
  526. #endif
  527.  
  528.     if (c == '.')                        /* insert last inserted text */
  529.         retval = stuff_inserted(NUL, 1L, TRUE);
  530.     else if (c == '%')                    /* insert file name */
  531.     {
  532.         if (check_fname() == FAIL)
  533.             return FAIL;
  534.         stuffReadbuff(curbuf->b_xfilename);
  535.     }
  536.     else if (c == ':')                    /* insert last command line */
  537.     {
  538.         if (last_cmdline == NULL)
  539.         {
  540.             EMSG(e_nolastcmd);
  541.             return FAIL;
  542.         }
  543.         stuffReadbuff(last_cmdline);
  544.     }
  545.     else                                /* name or number register */
  546.     {
  547.         yankbuffer = c;
  548.         get_yank_buffer(FALSE);
  549.         if (y_current->y_array == NULL)
  550.             retval = FAIL;
  551.         else
  552.         {
  553.  
  554.             for (i = 0; i < y_current->y_size; ++i)
  555.             {
  556.                 stuffReadbuff(y_current->y_array[i]);
  557.                 /* insert newline between lines and after last line if type is
  558.                  * MLINE */
  559.                 if (y_current->y_type == MLINE || i < y_current->y_size - 1)
  560.                     stuffReadbuff((char_u *)"\n");
  561.             }
  562.         }
  563.     }
  564.  
  565.     return retval;
  566. }
  567.  
  568. /*
  569.  * paste a yank buffer into the command line.
  570.  * used by CTRL-R command in command-line mode
  571.  * insertbuf() can't be used here, because special characters from the
  572.  * register contents will be interpreted as commands.
  573.  *
  574.  * return FAIL for failure, OK otherwise
  575.  */
  576.     int
  577. cmdline_paste(c)
  578.     int c;
  579. {
  580.     long i;
  581.  
  582.     if (!is_yank_buffer(c, FALSE))        /* check for valid buffer */
  583.         return FAIL;
  584.  
  585. #ifdef USE_GUI
  586.     if (c == '*')
  587.         gui_get_selection();
  588. #endif
  589.  
  590.     if (c == '.')                        /* insert last inserted text */
  591.         return FAIL;                    /* Unimplemented */
  592.  
  593.     if (c == '%')                        /* insert file name */
  594.     {
  595.         if (check_fname() == FAIL)
  596.             return FAIL;
  597.         return put_on_cmdline(curbuf->b_xfilename, -1, TRUE);
  598.     }
  599.  
  600.     if (c == ':')                        /* insert last command line */
  601.     {
  602.         if (last_cmdline == NULL)
  603.             return FAIL;
  604.         return put_on_cmdline(last_cmdline, -1, TRUE);
  605.     }
  606.  
  607.     yankbuffer = c;
  608.     get_yank_buffer(FALSE);
  609.     if (y_current->y_array == NULL)
  610.         return FAIL;
  611.  
  612.     for (i = 0; i < y_current->y_size; ++i)
  613.     {
  614.         put_on_cmdline(y_current->y_array[i], -1, FALSE);
  615.  
  616.         /* insert ^M between lines and after last line if type is MLINE */
  617.         if (y_current->y_type == MLINE || i < y_current->y_size - 1)
  618.             put_on_cmdline((char_u *)"\r", 1, FALSE);
  619.     }
  620.     return OK;
  621. }
  622.  
  623. /*
  624.  * do_delete - handle a delete operation
  625.  */
  626.     void
  627. do_delete()
  628. {
  629.     register int        n;
  630.     linenr_t            lnum;
  631.     char_u                *ptr;
  632.     char_u                *newp, *oldp;
  633.     linenr_t            old_lcount = curbuf->b_ml.ml_line_count;
  634.     int                    did_yank = FALSE;
  635.     struct block_def    bd;
  636.  
  637.     if (curbuf->b_ml.ml_flags & ML_EMPTY)            /* nothing to do */
  638.         return;
  639.  
  640. /*
  641.  * Imitate the strange Vi behaviour: If the delete spans more than one line
  642.  * and op_motion_type == MCHAR and the result is a blank line, make the delete
  643.  * linewise. Don't do this for the change command.
  644.  */
  645.     if (op_motion_type == MCHAR && op_line_count > 1 && op_type == DELETE)
  646.     {
  647.         ptr = ml_get(curbuf->b_op_end.lnum) + curbuf->b_op_end.col +
  648.                                                                  op_inclusive;
  649.         ptr = skipwhite(ptr);
  650.         if (*ptr == NUL && inindent(0))
  651.             op_motion_type = MLINE;
  652.     }
  653.  
  654. /*
  655.  * Check for trying to delete (e.g. "D") in an empty line.
  656.  * Note: For change command it is ok.
  657.  */
  658.     if (op_motion_type == MCHAR && op_line_count == 1 &&
  659.                 op_type == DELETE && *ml_get(curbuf->b_op_start.lnum) == NUL)
  660.     {
  661.         beep_flush();
  662.         return;
  663.     }
  664.  
  665. /*
  666.  * Do a yank of whatever we're about to delete.
  667.  * If a yank buffer was specified, put the deleted text into that buffer
  668.  */
  669.     if (yankbuffer != 0)
  670.     {
  671.                                         /* check for read-only buffer */
  672.         if (!is_yank_buffer(yankbuffer, TRUE))
  673.         {
  674.             beep_flush();
  675.             return;
  676.         }
  677.         get_yank_buffer(TRUE);            /* yank into specified buffer */
  678.         if (do_yank(TRUE, FALSE) == OK)    /* yank without message */
  679.             did_yank = TRUE;
  680.     }
  681.  
  682. /*
  683.  * Put deleted text into register 1 and shift number buffers if
  684.  * the delete contains a line break, or when a yankbuffer has been specified!
  685.  */
  686.     if (yankbuffer != 0 || op_motion_type == MLINE || op_line_count > 1)
  687.     {
  688.         y_current = &y_buf[9];
  689.         free_yank_all();                /* free buffer nine */
  690.         for (n = 9; n > 1; --n)
  691.             y_buf[n] = y_buf[n - 1];
  692.         y_previous = y_current = &y_buf[1];
  693.         y_buf[1].y_array = NULL;        /* set buffer one to empty */
  694.         yankbuffer = 0;
  695.     }
  696.     else if (yankbuffer == 0)            /* yank into unnamed buffer */
  697.     {
  698.         yankbuffer = '-';                /* use special delete buffer */
  699.         get_yank_buffer(TRUE);
  700.         yankbuffer = 0;
  701.     }
  702.  
  703.     if (yankbuffer == 0 && do_yank(TRUE, FALSE) == OK)
  704.         did_yank = TRUE;
  705.  
  706. /*
  707.  * If there's too much stuff to fit in the yank buffer, then get a
  708.  * confirmation before doing the delete. This is crude, but simple. And it
  709.  * avoids doing a delete of something we can't put back if we want.
  710.  */
  711.     if (!did_yank)
  712.     {
  713.         if (ask_yesno((char_u *)"cannot yank; delete anyway", TRUE) != 'y')
  714.         {
  715.             emsg(e_abort);
  716.             return;
  717.         }
  718.     }
  719.  
  720. /*
  721.  * block mode delete
  722.  */
  723.     if (op_block_mode)
  724.     {
  725.         if (u_save((linenr_t)(curbuf->b_op_start.lnum - 1),
  726.                                (linenr_t)(curbuf->b_op_end.lnum + 1)) == FAIL)
  727.             return;
  728.  
  729.         for (lnum = curwin->w_cursor.lnum;
  730.                                curwin->w_cursor.lnum <= curbuf->b_op_end.lnum;
  731.                                                       ++curwin->w_cursor.lnum)
  732.         {
  733.             block_prep(&bd, curwin->w_cursor.lnum, TRUE);
  734.             if (bd.textlen == 0)        /* nothing to delete */
  735.                 continue;
  736.  
  737.         /*
  738.          * If we delete a TAB, it may be replaced by several characters.
  739.          * Thus the number of characters may increase!
  740.          */
  741.             n = bd.textlen - bd.startspaces - bd.endspaces;        /* number of chars deleted */
  742.             oldp = ml_get_curline();
  743.             newp = alloc_check((unsigned)STRLEN(oldp) + 1 - n);
  744.             if (newp == NULL)
  745.                 continue;
  746.         /* copy up to deleted part */
  747.             vim_memmove(newp, oldp, (size_t)bd.textcol);
  748.         /* insert spaces */
  749.             copy_spaces(newp + bd.textcol, (size_t)(bd.startspaces + bd.endspaces));
  750.         /* copy the part after the deleted part */
  751.             oldp += bd.textcol + bd.textlen;
  752.             vim_memmove(newp + bd.textcol + bd.startspaces + bd.endspaces,
  753.                                                       oldp, STRLEN(oldp) + 1);
  754.         /* replace the line */
  755.             ml_replace(curwin->w_cursor.lnum, newp, FALSE);
  756.         }
  757.         curwin->w_cursor.lnum = lnum;
  758.         CHANGED;
  759.         updateScreen(VALID_TO_CURSCHAR);
  760.         op_line_count = 0;        /* no lines deleted */
  761.     }
  762.     else if (op_motion_type == MLINE)
  763.     {
  764.         if (op_type == CHANGE)
  765.         {
  766.                 /* Delete the lines except the first one.
  767.                  * Temporarily move the cursor to the next line.
  768.                  * Save the current line number, if the last line is deleted
  769.                  * it may be changed.
  770.                  */
  771.             if (op_line_count > 1)
  772.             {
  773.                 lnum = curwin->w_cursor.lnum;
  774.                 ++curwin->w_cursor.lnum;
  775.                 dellines((long)(op_line_count - 1), TRUE, TRUE);
  776.                 curwin->w_cursor.lnum = lnum;
  777.             }
  778.             if (u_save_cursor() == FAIL)
  779.                 return;
  780.             if (curbuf->b_p_ai)                /* don't delete indent */
  781.             {
  782.                 beginline(TRUE);            /* put cursor on first non-white */
  783.                 did_ai = TRUE;                /* delete the indent when ESC hit */
  784.             }
  785.             truncate_line(FALSE);
  786.             if (curwin->w_cursor.col > 0)
  787.                 --curwin->w_cursor.col;        /* put cursor on last char in line */
  788.         }
  789.         else
  790.         {
  791.             dellines(op_line_count, TRUE, TRUE);
  792.         }
  793.         u_clearline();    /* "U" command should not be possible after "dd" */
  794.         beginline(TRUE);
  795.     }
  796.     else if (op_line_count == 1)        /* delete characters within one line */
  797.     {
  798.         if (u_save_cursor() == FAIL)
  799.             return;
  800.             /* if 'cpoptions' contains '$', display '$' at end of change */
  801.         if (vim_strchr(p_cpo, CPO_DOLLAR) != NULL && op_type == CHANGE &&
  802.              curbuf->b_op_end.lnum == curwin->w_cursor.lnum && !op_is_VIsual)
  803.             display_dollar(curbuf->b_op_end.col - !op_inclusive);
  804.         n = curbuf->b_op_end.col - curbuf->b_op_start.col + 1 - !op_inclusive;
  805.         while (n-- > 0)
  806.             if (delchar(TRUE) == FAIL)
  807.                 break;
  808.     }
  809.     else                                /* delete characters between lines */
  810.     {
  811.         if (u_save_cursor() == FAIL)            /* save first line for undo */
  812.             return;
  813.         truncate_line(TRUE);            /* delete from cursor to end of line */
  814.  
  815.         curbuf->b_op_start = curwin->w_cursor;    /* remember curwin->w_cursor */
  816.         ++curwin->w_cursor.lnum;
  817.                                                 /* includes save for undo */
  818.         dellines((long)(op_line_count - 2), TRUE, TRUE);
  819.  
  820.         if (u_save_cursor() == FAIL)            /* save last line for undo */
  821.             return;
  822.         n = curbuf->b_op_end.col - !op_inclusive;
  823.         curwin->w_cursor.col = 0;
  824.         while (n-- >= 0)            /* delete from start of line until op_end */
  825.             if (delchar(TRUE) == FAIL)
  826.                 break;
  827.         curwin->w_cursor = curbuf->b_op_start;    /* restore curwin->w_cursor */
  828.         (void)do_join(FALSE, curs_rows() == OK);
  829.     }
  830.  
  831.     if ((op_motion_type == MCHAR && op_line_count == 1) || op_type == CHANGE)
  832.     {
  833.         if (dollar_vcol)
  834.             must_redraw = 0;        /* don't want a redraw now */
  835.         cursupdate();
  836.         if (!dollar_vcol)
  837.             updateline();
  838.     }
  839.     else if (!global_busy)            /* no need to update screen for :global */
  840.         updateScreen(CURSUPD);
  841.  
  842.     msgmore(curbuf->b_ml.ml_line_count - old_lcount);
  843.  
  844.         /* correct op_end for deleted text (for "']" command) */
  845.     if (op_block_mode)
  846.         curbuf->b_op_end.col = curbuf->b_op_start.col;
  847.     else
  848.         curbuf->b_op_end = curbuf->b_op_start;
  849. }
  850.  
  851. /*
  852.  * do_tilde - handle the (non-standard vi) tilde operator
  853.  */
  854.     void
  855. do_tilde()
  856. {
  857.     FPOS                pos;
  858.     struct block_def    bd;
  859.  
  860.     if (u_save((linenr_t)(curbuf->b_op_start.lnum - 1),
  861.                                (linenr_t)(curbuf->b_op_end.lnum + 1)) == FAIL)
  862.         return;
  863.  
  864.     pos = curbuf->b_op_start;
  865.     if (op_block_mode)                    /* Visual block mode */
  866.     {
  867.         for (; pos.lnum <= curbuf->b_op_end.lnum; ++pos.lnum)
  868.         {
  869.             block_prep(&bd, pos.lnum, FALSE);
  870.             pos.col = bd.textcol;
  871.             while (--bd.textlen >= 0)
  872.             {
  873.                 swapchar(&pos);
  874.                 if (inc(&pos) == -1)    /* at end of file */
  875.                     break;
  876.             }
  877.         }
  878.     }
  879.     else            /* not block mode */
  880.     {
  881.         if (op_motion_type == MLINE)
  882.         {
  883.                 pos.col = 0;
  884.                 curbuf->b_op_end.col = STRLEN(ml_get(curbuf->b_op_end.lnum));
  885.                 if (curbuf->b_op_end.col)
  886.                         --curbuf->b_op_end.col;
  887.         }
  888.         else if (!op_inclusive)
  889.             dec(&(curbuf->b_op_end));
  890.  
  891.         while (ltoreq(pos, curbuf->b_op_end))
  892.         {
  893.             swapchar(&pos);
  894.             if (inc(&pos) == -1)    /* at end of file */
  895.                 break;
  896.         }
  897.     }
  898.  
  899.     if (op_motion_type == MCHAR && op_line_count == 1 && !op_block_mode)
  900.     {
  901.         cursupdate();
  902.         updateline();
  903.     }
  904.     else
  905.         updateScreen(CURSUPD);
  906.  
  907.     if (op_line_count > p_report)
  908.             smsg((char_u *)"%ld line%s ~ed",
  909.                                         op_line_count, plural(op_line_count));
  910. }
  911.  
  912. /*
  913.  * If op_type == UPPER: make uppercase,
  914.  * if op_type == LOWER: make lowercase,
  915.  * else swap case of character at 'pos'
  916.  */
  917.     void
  918. swapchar(pos)
  919.     FPOS    *pos;
  920. {
  921.     int        c;
  922.  
  923.     c = gchar(pos);
  924.     if (islower(c) && op_type != LOWER)
  925.     {
  926.         pchar(*pos, toupper(c));
  927.         CHANGED;
  928.     }
  929.     else if (isupper(c) && op_type != UPPER)
  930.     {
  931.         pchar(*pos, tolower(c));
  932.         CHANGED;
  933.     }
  934. }
  935.  
  936. /*
  937.  * do_change - handle a change operation
  938.  * 
  939.  * return TRUE if edit() returns because of a CTRL-O command
  940.  */
  941.     int
  942. do_change()
  943. {
  944.     register colnr_t            l;
  945.  
  946.     l = curbuf->b_op_start.col;
  947.     if (op_motion_type == MLINE)
  948.     {
  949.         l = 0;
  950.         can_si = TRUE;        /* It's like opening a new line, do si */
  951.     }
  952.  
  953.     if (!op_empty)
  954.         do_delete();            /* delete the text and take care of undo */
  955.  
  956.     if ((l > curwin->w_cursor.col) && !lineempty(curwin->w_cursor.lnum))
  957.         inc_cursor();
  958.  
  959. #ifdef LISPINDENT
  960.     if (op_motion_type == MLINE)
  961.     {
  962.         if (curbuf->b_p_lisp && curbuf->b_p_ai)
  963.             fixthisline(get_lisp_indent);
  964. # ifdef CINDENT
  965.         else if (curbuf->b_p_cin)
  966.             fixthisline(get_c_indent);
  967. # endif
  968.     }
  969. #endif
  970.  
  971.     op_type = NOP;            /* don't want op_type == CHANGED in Insert mode */
  972.     return edit(NUL, FALSE, (linenr_t)1);
  973. }
  974.  
  975. /*
  976.  * set all the yank buffers to empty (called from main())
  977.  */
  978.     void
  979. init_yank()
  980. {
  981.     register int i;
  982.  
  983.     for (i = 0; i < NUM_REGISTERS; ++i)
  984.         y_buf[i].y_array = NULL;
  985. }
  986.  
  987. /*
  988.  * Free "n" lines from the current yank buffer.
  989.  * Called for normal freeing and in case of error.
  990.  */
  991.     static void
  992. free_yank(n)
  993.     long n;
  994. {
  995.     if (y_current->y_array != NULL)
  996.     {
  997.         register long i;
  998.  
  999.         for (i = n; --i >= 0; )
  1000.         {
  1001.             if ((i & 1023) == 1023)                    /* this may take a while */
  1002.             {
  1003.                 /*
  1004.                  * This message should never cause a hit-return message.
  1005.                  * Overwrite this message with any next message.
  1006.                  */
  1007.                 ++no_wait_return;
  1008.                 smsg((char_u *)"freeing %ld lines", i + 1);
  1009.                 --no_wait_return;
  1010.                 msg_didout = FALSE;
  1011.                 msg_col = 0;
  1012.             }
  1013.             vim_free(y_current->y_array[i]);
  1014.         }
  1015.         vim_free(y_current->y_array);
  1016.         y_current->y_array = NULL;
  1017.         if (n >= 1000)
  1018.             MSG("");
  1019.     }
  1020. }
  1021.  
  1022.     static void
  1023. free_yank_all()
  1024. {
  1025.         free_yank(y_current->y_size);
  1026. }
  1027.  
  1028. /*
  1029.  * Yank the text between curwin->w_cursor and startpos into a yank buffer.
  1030.  * If we are to append ("uppercase), we first yank into a new yank buffer and
  1031.  * then concatenate the old and the new one (so we keep the old one in case
  1032.  * of out-of-memory).
  1033.  *
  1034.  * return FAIL for failure, OK otherwise
  1035.  */
  1036.     int
  1037. do_yank(deleting, mess)
  1038.     int deleting;
  1039.     int mess;
  1040. {
  1041.     long                 i;                /* index in y_array[] */
  1042.     struct yankbuf        *curr;            /* copy of y_current */
  1043.     struct yankbuf        newbuf;         /* new yank buffer when appending */
  1044.     char_u                **new_ptr;
  1045.     register linenr_t    lnum;            /* current line number */
  1046.     long                 j;
  1047.     int                    yanktype = op_motion_type;
  1048.     long                yanklines = op_line_count;
  1049.     linenr_t            yankendlnum = curbuf->b_op_end.lnum;
  1050.  
  1051.     char_u                *pnew;
  1052.     struct block_def    bd;
  1053.  
  1054.                                     /* check for read-only buffer */
  1055.     if (yankbuffer != 0 && !is_yank_buffer(yankbuffer, TRUE))
  1056.     {
  1057.         beep_flush();
  1058.         return FAIL;
  1059.     }
  1060.     if (!deleting)                    /* do_delete() already set y_current */
  1061.         get_yank_buffer(TRUE);
  1062.  
  1063.     curr = y_current;
  1064.                                     /* append to existing contents */
  1065.     if (yankappend && y_current->y_array != NULL)
  1066.         y_current = &newbuf;
  1067.     else
  1068.         free_yank_all();        /* free previously yanked lines */
  1069.  
  1070. /*
  1071.  * If the cursor was in column 1 before and after the movement, and the
  1072.  * operator is not inclusive, the yank is always linewise.
  1073.  */
  1074.     if (op_motion_type == MCHAR && curbuf->b_op_start.col == 0 &&
  1075.                   !op_inclusive && curbuf->b_op_end.col == 0 && yanklines > 1)
  1076.     {
  1077.         yanktype = MLINE;
  1078.         --yankendlnum;
  1079.         --yanklines;
  1080.     }
  1081.  
  1082.     y_current->y_size = yanklines;
  1083.     y_current->y_type = yanktype;    /* set the yank buffer type */
  1084.     y_current->y_array = (char_u **)lalloc((long_u)(sizeof(char_u *) *
  1085.                                                             yanklines), TRUE);
  1086.  
  1087.     if (y_current->y_array == NULL)
  1088.     {
  1089.         y_current = curr;
  1090.         return FAIL;
  1091.     }
  1092.  
  1093.     i = 0;
  1094.     lnum = curbuf->b_op_start.lnum;
  1095.  
  1096. /*
  1097.  * Visual block mode
  1098.  */
  1099.     if (op_block_mode)
  1100.     {
  1101.         y_current->y_type = MBLOCK;    /* set the yank buffer type */
  1102.         for ( ; lnum <= yankendlnum; ++lnum)
  1103.         {
  1104.             block_prep(&bd, lnum, FALSE);
  1105.  
  1106.             if ((pnew = alloc(bd.startspaces + bd.endspaces +
  1107.                                           bd.textlen + 1)) == NULL)
  1108.                 goto fail;
  1109.             y_current->y_array[i++] = pnew;
  1110.  
  1111.             copy_spaces(pnew, (size_t)bd.startspaces);
  1112.             pnew += bd.startspaces;
  1113.  
  1114.             vim_memmove(pnew, bd.textstart, (size_t)bd.textlen);
  1115.             pnew += bd.textlen;
  1116.  
  1117.             copy_spaces(pnew, (size_t)bd.endspaces);
  1118.             pnew += bd.endspaces;
  1119.  
  1120.             *pnew = NUL;
  1121.         }
  1122.     }
  1123.     else
  1124.     {
  1125. /*
  1126.  * there are three parts for non-block mode:
  1127.  * 1. if yanktype != MLINE yank last part of the top line
  1128.  * 2. yank the lines between op_start and op_end, inclusive when
  1129.  *    yanktype == MLINE
  1130.  * 3. if yanktype != MLINE yank first part of the bot line
  1131.  */
  1132.         if (yanktype != MLINE)
  1133.         {
  1134.             if (yanklines == 1)        /* op_start and op_end on same line */
  1135.             {
  1136.                 j = curbuf->b_op_end.col - curbuf->b_op_start.col +
  1137.                                                             1 - !op_inclusive;
  1138.                 if ((y_current->y_array[0] = strnsave(ml_get(lnum) +
  1139.                                      curbuf->b_op_start.col, (int)j)) == NULL)
  1140.                 {
  1141. fail:
  1142.                     free_yank(i);    /* free the allocated lines */
  1143.                     y_current = curr;
  1144.                     return FAIL;
  1145.                 }
  1146.                 goto success;
  1147.             }
  1148.             if ((y_current->y_array[0] = strsave(ml_get(lnum++) +
  1149.                                              curbuf->b_op_start.col)) == NULL)
  1150.                 goto fail;
  1151.             ++i;
  1152.         }
  1153.  
  1154.         while (yanktype == MLINE ? (lnum <= yankendlnum) : (lnum < yankendlnum))
  1155.         {
  1156.             if ((y_current->y_array[i] = strsave(ml_get(lnum++))) == NULL)
  1157.                 goto fail;
  1158.             ++i;
  1159.         }
  1160.         if (yanktype != MLINE)
  1161.         {
  1162.             if ((y_current->y_array[i] = strnsave(ml_get(yankendlnum),
  1163.                            curbuf->b_op_end.col + 1 - !op_inclusive)) == NULL)
  1164.                 goto fail;
  1165.         }
  1166.     }
  1167.  
  1168. success:
  1169.     if (curr != y_current)        /* append the new block to the old block */
  1170.     {
  1171.         new_ptr = (char_u **)lalloc((long_u)(sizeof(char_u *) *
  1172.                                    (curr->y_size + y_current->y_size)), TRUE);
  1173.         if (new_ptr == NULL)
  1174.             goto fail;
  1175.         for (j = 0; j < curr->y_size; ++j)
  1176.             new_ptr[j] = curr->y_array[j];
  1177.         vim_free(curr->y_array);
  1178.         curr->y_array = new_ptr;
  1179.  
  1180.         if (yanktype == MLINE)     /* MLINE overrides MCHAR and MBLOCK */
  1181.             curr->y_type = MLINE;
  1182.  
  1183.         /* concatenate the last line of the old block with the first line of
  1184.          * the new block */
  1185.         if (curr->y_type == MCHAR)
  1186.         {
  1187.             pnew = lalloc((long_u)(STRLEN(curr->y_array[curr->y_size - 1])
  1188.                               + STRLEN(y_current->y_array[0]) + 1), TRUE);
  1189.             if (pnew == NULL)
  1190.             {
  1191.                     i = y_current->y_size - 1;
  1192.                     goto fail;
  1193.             }
  1194.             STRCPY(pnew, curr->y_array[--j]);
  1195.             STRCAT(pnew, y_current->y_array[0]);
  1196.             vim_free(curr->y_array[j]);
  1197.             vim_free(y_current->y_array[0]);
  1198.             curr->y_array[j++] = pnew;
  1199.             i = 1;
  1200.         }
  1201.         else
  1202.             i = 0;
  1203.         while (i < y_current->y_size)
  1204.             curr->y_array[j++] = y_current->y_array[i++];
  1205.         curr->y_size = j;
  1206.         vim_free(y_current->y_array);
  1207.         y_current = curr;
  1208.     }
  1209.     if (mess)                    /* Display message about yank? */
  1210.     {
  1211.         if (yanktype == MCHAR && !op_block_mode)
  1212.             --yanklines;
  1213.         if (yanklines > p_report)
  1214.         {
  1215.             cursupdate();        /* redisplay now, so message is not deleted */
  1216.             smsg((char_u *)"%ld line%s yanked", yanklines, plural(yanklines));
  1217.         }
  1218.     }
  1219.  
  1220.     return OK;
  1221. }
  1222.  
  1223. /*
  1224.  * put contents of register into the text
  1225.  * For ":put" command count == -1.
  1226.  */
  1227.     void
  1228. do_put(dir, count, fix_indent)
  1229.     int        dir;                /* BACKWARD for 'P', FORWARD for 'p' */
  1230.     long    count;
  1231.     int        fix_indent;            /* make indent look nice */
  1232. {
  1233.     char_u        *ptr;
  1234.     char_u        *newp, *oldp;
  1235.     int         yanklen;
  1236.     int            oldlen;
  1237.     int            totlen = 0;                    /* init for gcc */
  1238.     linenr_t    lnum;
  1239.     colnr_t        col;
  1240.     long         i;                            /* index in y_array[] */
  1241.     int         y_type;
  1242.     long         y_size;
  1243.     char_u        **y_array;
  1244.     long         nr_lines = 0;
  1245.     colnr_t        vcol;
  1246.     int            delcount;
  1247.     int            incr = 0;
  1248.     long        j;
  1249.     FPOS        new_cursor;
  1250.     int            indent;
  1251.     int            orig_indent = 0;            /* init for gcc */
  1252.     int            indent_diff = 0;            /* init for gcc */
  1253.     int            first_indent = TRUE;
  1254.     FPOS        old_pos;
  1255.     struct block_def bd;
  1256.     char_u        *insert_string = NULL;
  1257.  
  1258. #ifdef USE_GUI
  1259.     if (yankbuffer == '*')
  1260.         gui_get_selection();
  1261. #endif
  1262.  
  1263.     if (fix_indent)
  1264.         orig_indent = get_indent();
  1265.  
  1266.     curbuf->b_op_start = curwin->w_cursor;        /* default for "'[" command */
  1267.     if (dir == FORWARD)
  1268.         curbuf->b_op_start.col++;
  1269.     curbuf->b_op_end = curwin->w_cursor;        /* default for "']" command */
  1270.  
  1271.     /*
  1272.      * Using inserted text works differently, because the buffer includes
  1273.      * special characters (newlines, etc.).
  1274.      */
  1275.     if (yankbuffer == '.')
  1276.     {
  1277.         (void)stuff_inserted((dir == FORWARD ? (count == -1 ? 'o' : 'a') :
  1278.                                     (count == -1 ? 'O' : 'i')), count, FALSE);
  1279.         return;
  1280.     }
  1281.  
  1282.     /*
  1283.      * For '%' (file name) and ':' (last command line) we have to create a
  1284.      * fake yank buffer.
  1285.      */
  1286.     if (yankbuffer == '%')                /* use file name */
  1287.     {
  1288.         if (check_fname() == FAIL)
  1289.             return;
  1290.         insert_string = curbuf->b_xfilename;
  1291.     }
  1292.     else if (yankbuffer == ':')            /* use last command line */
  1293.     {
  1294.         if (last_cmdline == NULL)
  1295.         {
  1296.             EMSG(e_nolastcmd);
  1297.             return;
  1298.         }
  1299.         insert_string = last_cmdline;
  1300.     }
  1301.  
  1302.     if (insert_string != NULL)
  1303.     {
  1304.         y_type = MCHAR;                    /* use fake one-line yank buffer */
  1305.         y_size = 1;
  1306.         y_array = &insert_string;
  1307.     }
  1308.     else
  1309.     {
  1310.         get_yank_buffer(FALSE);
  1311.  
  1312.         y_type = y_current->y_type;
  1313.         y_size = y_current->y_size;
  1314.         y_array = y_current->y_array;
  1315.     }
  1316.  
  1317.     if (count == -1)        /* :put command */
  1318.     {
  1319.         y_type = MLINE;
  1320.         count = 1;
  1321.     }
  1322.  
  1323.     if (y_size == 0 || y_array == NULL)
  1324.     {
  1325.         EMSG2("Nothing in register %s", transchar(yankbuffer));
  1326.         return;
  1327.     }
  1328.  
  1329.     if (y_type == MBLOCK)
  1330.     {
  1331.         lnum = curwin->w_cursor.lnum + y_size + 1;
  1332.         if (lnum > curbuf->b_ml.ml_line_count)
  1333.             lnum = curbuf->b_ml.ml_line_count + 1;
  1334.         if (u_save(curwin->w_cursor.lnum - 1, lnum) == FAIL)
  1335.             return;
  1336.     }
  1337.     else if (u_save_cursor() == FAIL)
  1338.         return;
  1339.  
  1340.     yanklen = STRLEN(y_array[0]);
  1341.     CHANGED;
  1342.  
  1343.     lnum = curwin->w_cursor.lnum;
  1344.     col = curwin->w_cursor.col;
  1345.  
  1346. /*
  1347.  * block mode
  1348.  */
  1349.     if (y_type == MBLOCK)
  1350.     {
  1351.         if (dir == FORWARD && gchar_cursor() != NUL)
  1352.         {
  1353.             getvcol(curwin, &curwin->w_cursor, NULL, NULL, &col);
  1354.             ++col;
  1355.             ++curwin->w_cursor.col;
  1356.         }
  1357.         else
  1358.             getvcol(curwin, &curwin->w_cursor, &col, NULL, NULL);
  1359.         for (i = 0; i < y_size; ++i)
  1360.         {
  1361.             bd.startspaces = 0;
  1362.             bd.endspaces = 0;
  1363.             bd.textcol = 0;
  1364.             vcol = 0;
  1365.             delcount = 0;
  1366.  
  1367.         /* add a new line */
  1368.             if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
  1369.             {
  1370.                 ml_append(curbuf->b_ml.ml_line_count, (char_u *)"",
  1371.                                                            (colnr_t)1, FALSE);
  1372.                 ++nr_lines;
  1373.             }
  1374.             oldp = ml_get_curline();
  1375.             oldlen = STRLEN(oldp);
  1376.             for (ptr = oldp; vcol < col && *ptr; ++ptr)
  1377.             {
  1378.                 /* Count a tab for what it's worth (if list mode not on) */
  1379.                 incr = lbr_chartabsize(ptr, (colnr_t)vcol);
  1380.                 vcol += incr;
  1381.                 ++bd.textcol;
  1382.             }
  1383.             if (vcol < col)    /* line too short, padd with spaces */
  1384.             {
  1385.                 bd.startspaces = col - vcol;
  1386.             }
  1387.             else if (vcol > col)
  1388.             {
  1389.                 bd.endspaces = vcol - col;
  1390.                 bd.startspaces = incr - bd.endspaces;
  1391.                 --bd.textcol;
  1392.                 delcount = 1;
  1393.             }
  1394.             yanklen = STRLEN(y_array[i]);
  1395.             totlen = count * yanklen + bd.startspaces + bd.endspaces;
  1396.             newp = alloc_check((unsigned)totlen + oldlen + 1);
  1397.             if (newp == NULL)
  1398.                 break;
  1399.         /* copy part up to cursor to new line */
  1400.             ptr = newp;
  1401.             vim_memmove(ptr, oldp, (size_t)bd.textcol);
  1402.             ptr += bd.textcol;
  1403.         /* may insert some spaces before the new text */
  1404.             copy_spaces(ptr, (size_t)bd.startspaces);
  1405.             ptr += bd.startspaces;
  1406.         /* insert the new text */
  1407.             for (j = 0; j < count; ++j)
  1408.             {
  1409.                 vim_memmove(ptr, y_array[i], (size_t)yanklen);
  1410.                 ptr += yanklen;
  1411.             }
  1412.         /* may insert some spaces after the new text */
  1413.             copy_spaces(ptr, (size_t)bd.endspaces);
  1414.             ptr += bd.endspaces;
  1415.         /* move the text after the cursor to the end of the line. */
  1416.             vim_memmove(ptr, oldp + bd.textcol + delcount,
  1417.                                 (size_t)(oldlen - bd.textcol - delcount + 1));
  1418.             ml_replace(curwin->w_cursor.lnum, newp, FALSE);
  1419.  
  1420.             ++curwin->w_cursor.lnum;
  1421.             if (i == 0)
  1422.                 curwin->w_cursor.col += bd.startspaces;
  1423.         }
  1424.                                                 /* for "']" command */
  1425.         curbuf->b_op_end.lnum = curwin->w_cursor.lnum - 1;
  1426.         curbuf->b_op_end.col = bd.textcol + totlen - 1;
  1427.         curwin->w_cursor.lnum = lnum;
  1428.         cursupdate();
  1429.         updateScreen(VALID_TO_CURSCHAR);
  1430.     }
  1431.     else        /* not block mode */
  1432.     {
  1433.         if (y_type == MCHAR)
  1434.         {
  1435.     /* if type is MCHAR, FORWARD is the same as BACKWARD on the next char */
  1436.             if (dir == FORWARD && gchar_cursor() != NUL)
  1437.             {
  1438.                 ++col;
  1439.                 if (yanklen)
  1440.                 {
  1441.                     ++curwin->w_cursor.col;
  1442.                     ++curbuf->b_op_end.col;
  1443.                 }
  1444.             }
  1445.             new_cursor = curwin->w_cursor;
  1446.         }
  1447.         else if (dir == BACKWARD)
  1448.     /* if type is MLINE, BACKWARD is the same as FORWARD on the previous line */
  1449.             --lnum;
  1450.  
  1451. /*
  1452.  * simple case: insert into current line
  1453.  */
  1454.         if (y_type == MCHAR && y_size == 1)
  1455.         {
  1456.             totlen = count * yanklen;
  1457.             if (totlen)
  1458.             {
  1459.                 oldp = ml_get(lnum);
  1460.                 newp = alloc_check((unsigned)(STRLEN(oldp) + totlen + 1));
  1461.                 if (newp == NULL)
  1462.                     return;             /* alloc() will give error message */
  1463.                 vim_memmove(newp, oldp, (size_t)col);
  1464.                 ptr = newp + col;
  1465.                 for (i = 0; i < count; ++i)
  1466.                 {
  1467.                     vim_memmove(ptr, y_array[0], (size_t)yanklen);
  1468.                     ptr += yanklen;
  1469.                 }
  1470.                 vim_memmove(ptr, oldp + col, STRLEN(oldp + col) + 1);
  1471.                 ml_replace(lnum, newp, FALSE);
  1472.                                           /* put cursor on last putted char */
  1473.                 curwin->w_cursor.col += (colnr_t)(totlen - 1);
  1474.             }
  1475.             curbuf->b_op_end = curwin->w_cursor;
  1476.             updateline();
  1477.         }
  1478.         else
  1479.         {
  1480.             while (--count >= 0)
  1481.             {
  1482.                 i = 0;
  1483.                 if (y_type == MCHAR)
  1484.                 {
  1485.                     /*
  1486.                      * Split the current line in two at the insert position.
  1487.                      * First insert y_array[size - 1] in front of second line.
  1488.                      * Then append y_array[0] to first line.
  1489.                      */
  1490.                     ptr = ml_get(lnum) + col;
  1491.                     totlen = STRLEN(y_array[y_size - 1]);
  1492.                     newp = alloc_check((unsigned)(STRLEN(ptr) + totlen + 1));
  1493.                     if (newp == NULL)
  1494.                         goto error;
  1495.                     STRCPY(newp, y_array[y_size - 1]);
  1496.                     STRCAT(newp, ptr);
  1497.                         /* insert second line */
  1498.                     ml_append(lnum, newp, (colnr_t)0, FALSE);
  1499.                     vim_free(newp);
  1500.  
  1501.                     oldp = ml_get(lnum);
  1502.                     newp = alloc_check((unsigned)(col + yanklen + 1));
  1503.                     if (newp == NULL)
  1504.                         goto error;
  1505.                                             /* copy first part of line */
  1506.                     vim_memmove(newp, oldp, (size_t)col);
  1507.                                             /* append to first line */
  1508.                     vim_memmove(newp + col, y_array[0], (size_t)(yanklen + 1));
  1509.                     ml_replace(lnum, newp, FALSE);
  1510.  
  1511.                     curwin->w_cursor.lnum = lnum;
  1512.                     i = 1;
  1513.                 }
  1514.  
  1515.                 while (i < y_size)
  1516.                 {
  1517.                     if ((y_type != MCHAR || i < y_size - 1) &&
  1518.                         ml_append(lnum, y_array[i], (colnr_t)0, FALSE) == FAIL)
  1519.                             goto error;
  1520.                     lnum++;
  1521.                     i++;
  1522.                     if (fix_indent)
  1523.                     {
  1524.                         old_pos = curwin->w_cursor;
  1525.                         curwin->w_cursor.lnum = lnum;
  1526.                         ptr = ml_get(lnum);
  1527. #if defined(SMARTINDENT) || defined(CINDENT)
  1528.                         if (*ptr == '#'
  1529. # ifdef SMARTINDENT
  1530.                            && curbuf->b_p_si
  1531. # endif
  1532. # ifdef CINDENT
  1533.                            && curbuf->b_p_cin && in_cinkeys('#', ' ', TRUE)
  1534. # endif
  1535.                                             )
  1536.                     
  1537.                             indent = 0;     /* Leave # lines at start */
  1538.                         else
  1539. #endif
  1540.                              if (*ptr == NUL)
  1541.                             indent = 0;     /* Ignore empty lines */
  1542.                         else if (first_indent)
  1543.                         {
  1544.                             indent_diff = orig_indent - get_indent();
  1545.                             indent = orig_indent;
  1546.                             first_indent = FALSE;
  1547.                         }
  1548.                         else if ((indent = get_indent() + indent_diff) < 0)
  1549.                             indent = 0;
  1550.                         set_indent(indent, TRUE);
  1551.                         curwin->w_cursor = old_pos;
  1552.                     }
  1553.                     ++nr_lines;
  1554.                 }
  1555.             }
  1556.  
  1557.             /* put '] at last inserted character */
  1558.             curbuf->b_op_end.lnum = lnum;
  1559.             col = STRLEN(y_array[y_size - 1]);
  1560.             if (col > 1)
  1561.                 curbuf->b_op_end.col = col - 1;
  1562.             else
  1563.                 curbuf->b_op_end.col = 0;
  1564.  
  1565.             if (y_type == MLINE)
  1566.             {
  1567.                 curwin->w_cursor.col = 0;
  1568.                 if (dir == FORWARD)
  1569.                 {
  1570.                     updateScreen(NOT_VALID);    /* recomp. curwin->w_botline */
  1571.                     ++curwin->w_cursor.lnum;
  1572.                 }
  1573.                     /* put cursor on first non-blank in last inserted line */
  1574.                 beginline(TRUE);
  1575.             }
  1576.             else        /* put cursor on first inserted character */
  1577.             {
  1578.                 curwin->w_cursor = new_cursor;
  1579.             }
  1580.  
  1581. error:
  1582.             if (y_type == MLINE)        /* for '[ */
  1583.             {
  1584.                 curbuf->b_op_start.col = 0;
  1585.                 if (dir == FORWARD)
  1586.                     curbuf->b_op_start.lnum++;
  1587.             }
  1588.             mark_adjust(curbuf->b_op_start.lnum + (y_type == MCHAR),
  1589.                                                        MAXLNUM, nr_lines, 0L);
  1590.             updateScreen(CURSUPD);
  1591.         }
  1592.     }
  1593.  
  1594.     msgmore(nr_lines);
  1595.     curwin->w_set_curswant = TRUE;
  1596. }
  1597.  
  1598. /* Return the character name of the register with the given number */
  1599.     int
  1600. get_register_name(num)
  1601.     int num;
  1602. {
  1603.     if (num == -1)
  1604.         return '"';
  1605.     else if (num < 10)
  1606.         return num + '0';
  1607.     else if (num == DELETION_REGISTER)
  1608.         return '-';
  1609. #ifdef USE_GUI
  1610.     else if (num == GUI_SELECTION_REGISTER)
  1611.         return '*';
  1612. #endif
  1613.     else
  1614.         return num + 'a' - 10;
  1615. }
  1616.  
  1617. /*
  1618.  * display the contents of the yank buffers
  1619.  */
  1620.     void
  1621. do_dis(arg)
  1622.     char_u *arg;
  1623. {
  1624.     register int            i, n;
  1625.     register long            j;
  1626.     register char_u            *p;
  1627.     register struct yankbuf *yb;
  1628.     char_u name;
  1629.   
  1630.     if (arg != NULL && *arg == NUL)
  1631.         arg = NULL;
  1632.  
  1633.     set_highlight('t');        /* Highlight title */
  1634.     start_highlight();
  1635.     MSG_OUTSTR("\n--- Registers ---");
  1636.     stop_highlight();
  1637.     for (i = -1; i < NUM_REGISTERS; ++i)
  1638.     {
  1639.         if (i == -1)
  1640.         {
  1641.             if (y_previous != NULL)
  1642.                 yb = y_previous;
  1643.             else
  1644.                 yb = &(y_buf[0]);
  1645.         }
  1646.         else
  1647.             yb = &(y_buf[i]);
  1648.         name = get_register_name(i);
  1649.         if (yb->y_array != NULL && (arg == NULL ||
  1650.                                                vim_strchr(arg, name) != NULL))
  1651.         {
  1652.             msg_outchar('\n');
  1653.             msg_outchar('"');
  1654.             msg_outchar(name);
  1655.             MSG_OUTSTR("   ");
  1656.  
  1657.             n = (int)Columns - 6;
  1658.             for (j = 0; j < yb->y_size && n > 1; ++j)
  1659.             {
  1660.                 if (j)
  1661.                 {
  1662.                     MSG_OUTSTR("^J");
  1663.                     n -= 2;
  1664.                 }
  1665.                 for (p = yb->y_array[j]; *p && (n -= charsize(*p)) >= 0; ++p)
  1666.                     msg_outtrans_len(p, 1);
  1667.             }
  1668.             flushbuf();                /* show one line at a time */
  1669.         }
  1670.     }
  1671.  
  1672.     /*
  1673.      * display last inserted text
  1674.      */
  1675.     if ((p = get_last_insert()) != NULL &&
  1676.         (arg == NULL || vim_strchr(arg, '.') != NULL))
  1677.     {
  1678.         MSG_OUTSTR("\n\".   ");
  1679.         dis_msg(p, TRUE);
  1680.     }
  1681.  
  1682.     /*
  1683.      * display last command line
  1684.      */
  1685.     if (last_cmdline != NULL && (arg == NULL || vim_strchr(arg, ':') != NULL))
  1686.     {
  1687.         MSG_OUTSTR("\n\":   ");
  1688.         dis_msg(last_cmdline, FALSE);
  1689.     }
  1690.  
  1691.     /*
  1692.      * display current file name
  1693.      */
  1694.     if (curbuf->b_xfilename != NULL &&
  1695.                                 (arg == NULL || vim_strchr(arg, '%') != NULL))
  1696.     {
  1697.         MSG_OUTSTR("\n\"%   ");
  1698.         dis_msg(curbuf->b_xfilename, FALSE);
  1699.     }
  1700. }
  1701.  
  1702. /*
  1703.  * display a string for do_dis()
  1704.  * truncate at end of screen line
  1705.  */
  1706.     void
  1707. dis_msg(p, skip_esc)
  1708.     char_u        *p;
  1709.     int            skip_esc;            /* if TRUE, ignore trailing ESC */
  1710. {
  1711.     int        n;
  1712.  
  1713.     n = (int)Columns - 6;
  1714.     while (*p && !(*p == ESC && skip_esc && *(p + 1) == NUL) &&
  1715.                         (n -= charsize(*p)) >= 0)
  1716.         msg_outtrans_len(p++, 1);
  1717. }
  1718.  
  1719. /*
  1720.  * join 'count' lines (minimal 2), including u_save()
  1721.  */
  1722.     void
  1723. do_do_join(count, insert_space, redraw)
  1724.     long    count;
  1725.     int        insert_space;
  1726.     int        redraw;                    /* can redraw, curwin->w_col valid */
  1727. {
  1728.     if (u_save((linenr_t)(curwin->w_cursor.lnum - 1),
  1729.                     (linenr_t)(curwin->w_cursor.lnum + count)) == FAIL)
  1730.         return;
  1731.  
  1732.     if (count > 10)
  1733.         redraw = FALSE;                /* don't redraw each small change */
  1734.     while (--count > 0)
  1735.     {
  1736.         line_breakcheck();
  1737.         if (got_int || do_join(insert_space, redraw) == FAIL)
  1738.         {
  1739.             beep_flush();
  1740.             break;
  1741.         }
  1742.     }
  1743.     if (redraw)
  1744.         redraw_later(VALID_TO_CURSCHAR);
  1745.     else
  1746.         redraw_later(NOT_VALID);
  1747.     
  1748.     /*
  1749.      * Need to update the screen if the line where the cursor is became too
  1750.      * long to fit on the screen.
  1751.      */
  1752.     cursupdate();
  1753. }
  1754.  
  1755. /*
  1756.  * Join two lines at the cursor position.
  1757.  *
  1758.  * return FAIL for failure, OK ohterwise
  1759.  */
  1760.     int
  1761. do_join(insert_space, redraw)
  1762.     int            insert_space;
  1763.     int            redraw;        /* should only be TRUE when curwin->w_row valid */
  1764. {
  1765.     char_u        *curr;
  1766.     char_u        *next;
  1767.     char_u        *newp;
  1768.     int            endcurr1, endcurr2;
  1769.     int         currsize;        /* size of the current line */
  1770.     int         nextsize;        /* size of the next line */
  1771.     int            spaces;            /* number of spaces to insert */
  1772.     int            rows_to_del = 0;/* number of rows on screen to delete */
  1773.     linenr_t    t;
  1774.  
  1775.     if (curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
  1776.         return FAIL;            /* can't join on last line */
  1777.  
  1778.     if (redraw)
  1779.         rows_to_del = plines_m(curwin->w_cursor.lnum,
  1780.                                                    curwin->w_cursor.lnum + 1);
  1781.  
  1782.     curr = ml_get_curline();
  1783.     currsize = STRLEN(curr);
  1784.     endcurr1 = endcurr2 = NUL;
  1785.     if (currsize > 0)
  1786.     {
  1787.         endcurr1 = *(curr + currsize - 1);
  1788.         if (currsize > 1)
  1789.             endcurr2 = *(curr + currsize - 2);
  1790.     }
  1791.  
  1792.     next = ml_get((linenr_t)(curwin->w_cursor.lnum + 1));
  1793.     spaces = 0;
  1794.     if (insert_space)
  1795.     {
  1796.         next = skipwhite(next);
  1797.         spaces = 1;
  1798.         if (*next == ')' || currsize == 0)
  1799.             spaces = 0;
  1800.         else
  1801.         {
  1802.             if (endcurr1 == ' ' || endcurr1 == TAB)
  1803.             {
  1804.                 spaces = 0;
  1805.                 if (currsize > 1)
  1806.                     endcurr1 = endcurr2;
  1807.             }
  1808.             if (p_js && vim_strchr((char_u *)".!?", endcurr1) != NULL)
  1809.                 spaces = 2;
  1810.         }
  1811.     }
  1812.     nextsize = STRLEN(next);
  1813.  
  1814.     newp = alloc_check((unsigned)(currsize + nextsize + spaces + 1));
  1815.     if (newp == NULL)
  1816.         return FAIL;
  1817.  
  1818.     /*
  1819.      * Insert the next line first, because we already have that pointer.
  1820.      * Curr has to be obtained again, because getting next will have
  1821.      * invalidated it.
  1822.      */
  1823.     vim_memmove(newp + currsize + spaces, next, (size_t)(nextsize + 1));
  1824.  
  1825.     curr = ml_get_curline();
  1826.     vim_memmove(newp, curr, (size_t)currsize);
  1827.  
  1828.     copy_spaces(newp + currsize, (size_t)spaces);
  1829.  
  1830.     ml_replace(curwin->w_cursor.lnum, newp, FALSE);
  1831.  
  1832.     /*
  1833.      * Delete the following line. To do this we move the cursor there
  1834.      * briefly, and then move it back. After dellines() the cursor may
  1835.      * have moved up (last line deleted), so the current lnum is kept in t.
  1836.      */
  1837.     t = curwin->w_cursor.lnum;
  1838.     ++curwin->w_cursor.lnum;
  1839.     dellines(1L, FALSE, FALSE);
  1840.     curwin->w_cursor.lnum = t;
  1841.  
  1842.     /*
  1843.      * the number of rows on the screen is reduced by the difference
  1844.      * in number of rows of the two old lines and the one new line
  1845.      */
  1846.     if (redraw)
  1847.     {
  1848.         rows_to_del -= plines(curwin->w_cursor.lnum);
  1849.         if (rows_to_del > 0)
  1850.             win_del_lines(curwin, curwin->w_cline_row + curwin->w_cline_height,
  1851.                                                      rows_to_del, TRUE, TRUE);
  1852.     }
  1853.  
  1854.      /*
  1855.      * go to first character of the joined line
  1856.      */
  1857.     if (currsize == 0)
  1858.         curwin->w_cursor.col = 0;
  1859.     else
  1860.     {
  1861.         curwin->w_cursor.col = currsize - 1;
  1862.         (void)oneright();
  1863.     }
  1864.     CHANGED;
  1865.  
  1866.     return OK;
  1867. }
  1868.  
  1869. /*
  1870.  * Return TRUE if the two comment leaders given are the same.  The cursor is
  1871.  * in the first line.  White-space is ignored.  Note that the whole of
  1872.  * 'leader1' must match 'leader2_len' characters from 'leader2' -- webb
  1873.  */
  1874.     static int
  1875. same_leader(leader1_len, leader1_flags, leader2_len, leader2_flags)
  1876.     int        leader1_len;
  1877.     char_u    *leader1_flags;
  1878.     int        leader2_len;
  1879.     char_u    *leader2_flags;
  1880. {
  1881.     int        idx1 = 0, idx2 = 0;
  1882.     char_u    *p;
  1883.     char_u    *line1;
  1884.     char_u    *line2;
  1885.  
  1886.     if (leader1_len == 0)
  1887.         return (leader2_len == 0);
  1888.  
  1889.     /*
  1890.      * If first leader has 'f' flag, the lines can be joined only if the
  1891.      * second line does not have a leader.
  1892.      * If first leader has 'e' flag, the lines can never be joined.
  1893.      * If fist leader has 's' flag, the lines can only be joined if there is
  1894.      * some text after it and the second line has the 'm' flag.
  1895.      */
  1896.     if (leader1_flags != NULL)
  1897.     {
  1898.         for (p = leader1_flags; *p && *p != ':'; ++p)
  1899.         {
  1900.             if (*p == COM_FIRST)
  1901.                 return (leader2_len == 0);
  1902.             if (*p == COM_END)
  1903.                 return FALSE;
  1904.             if (*p == COM_START)
  1905.             {
  1906.                 if (*(ml_get_curline() + leader1_len) == NUL)
  1907.                     return FALSE;
  1908.                 if (leader2_flags == NULL || leader2_len == 0)
  1909.                     return FALSE;
  1910.                 for (p = leader2_flags; *p && *p != ':'; ++p)
  1911.                     if (*p == COM_MIDDLE)
  1912.                         return TRUE;
  1913.                 return FALSE;
  1914.             }
  1915.         }
  1916.     }
  1917.  
  1918.     /*
  1919.      * Get current line and next line, compare the leaders.
  1920.      * The first line has to be saved, only one line can be locked at a time.
  1921.      */
  1922.     line1 = strsave(ml_get_curline());
  1923.     if (line1 != NULL)
  1924.     {
  1925.         for (idx1 = 0; vim_iswhite(line1[idx1]); ++idx1)
  1926.             ;
  1927.         line2 = ml_get(curwin->w_cursor.lnum + 1);
  1928.         for (idx2 = 0; idx2 < leader2_len; ++idx2)
  1929.         {
  1930.             if (!vim_iswhite(line2[idx2]))
  1931.             {
  1932.                 if (line1[idx1++] != line2[idx2])
  1933.                     break;
  1934.             }
  1935.             else
  1936.                 while (vim_iswhite(line1[idx1]))
  1937.                     ++idx1;
  1938.         }
  1939.         vim_free(line1);
  1940.     }
  1941.     return (idx2 == leader2_len && idx1 == leader1_len);
  1942. }
  1943.  
  1944. /*
  1945.  * implementation of the format operator 'Q'
  1946.  */
  1947.     void
  1948. do_format()
  1949. {
  1950.     long        old_line_count = curbuf->b_ml.ml_line_count;
  1951.     int            prev_is_blank = FALSE;
  1952.     int            is_end_block = TRUE;
  1953.     int            next_is_end_block;
  1954.     int            leader_len = 0;        /* init for gcc */
  1955.     int            next_leader_len;
  1956.     char_u        *leader_flags = NULL;
  1957.     char_u        *next_leader_flags;
  1958.     int            advance = TRUE;
  1959.     int            second_indent = -1;
  1960.     int            do_second_indent;
  1961.     int            first_par_line = TRUE;
  1962.  
  1963.     if (u_save((linenr_t)(curwin->w_cursor.lnum - 1),
  1964.                    (linenr_t)(curwin->w_cursor.lnum + op_line_count)) == FAIL)
  1965.         return;
  1966.  
  1967.     /* check for 'q' and '2' in 'formatoptions' */
  1968.     fo_do_comments = has_format_option(FO_Q_COMS);
  1969.     do_second_indent = has_format_option(FO_Q_SECOND);
  1970.  
  1971.     /*
  1972.      * get info about the previous and current line.
  1973.      */
  1974.     if (curwin->w_cursor.lnum > 1)
  1975.         is_end_block = fmt_end_block(curwin->w_cursor.lnum - 1,
  1976.                                         &next_leader_len, &next_leader_flags);
  1977.     next_is_end_block = fmt_end_block(curwin->w_cursor.lnum,
  1978.                                         &next_leader_len, &next_leader_flags);
  1979.  
  1980.     curwin->w_cursor.lnum--;
  1981.     while (--op_line_count >= 0)
  1982.     {
  1983.         /*
  1984.          * Advance to next block.
  1985.          */
  1986.         if (advance)
  1987.         {
  1988.             curwin->w_cursor.lnum++;
  1989.             prev_is_blank = is_end_block;
  1990.             is_end_block = next_is_end_block;
  1991.             leader_len = next_leader_len;
  1992.             leader_flags = next_leader_flags;
  1993.         }
  1994.  
  1995.         /*
  1996.          * The last line to be formatted.
  1997.          */
  1998.         if (op_line_count == 0)
  1999.         {
  2000.             next_is_end_block = TRUE;
  2001.             next_leader_len = 0;
  2002.             next_leader_flags = NULL;
  2003.         }
  2004.         else
  2005.             next_is_end_block = fmt_end_block(curwin->w_cursor.lnum + 1,
  2006.                                         &next_leader_len, &next_leader_flags);
  2007.         advance = TRUE;
  2008.  
  2009.         /*
  2010.          * For the first line of a paragraph, check indent of second line.
  2011.          * Don't do this for comments and empty lines.
  2012.          */
  2013.         if (first_par_line && do_second_indent &&
  2014.                 prev_is_blank && !is_end_block &&
  2015.                 curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count &&
  2016.                 leader_len == 0 && next_leader_len == 0 &&
  2017.                 !lineempty(curwin->w_cursor.lnum + 1))
  2018.             second_indent = get_indent_lnum(curwin->w_cursor.lnum + 1);
  2019.  
  2020.         /*
  2021.          * Skip end-of-block (blank) lines
  2022.          */
  2023.         if (is_end_block)
  2024.         {
  2025.         }
  2026.         /*
  2027.          * If we have got to the end of a paragraph, format it.
  2028.          */
  2029.         else if (next_is_end_block || !same_leader(leader_len, leader_flags,
  2030.                                           next_leader_len, next_leader_flags))
  2031.         {
  2032.             /* replace indent in first line with minimal number of tabs and
  2033.              * spaces, according to current options */
  2034.             set_indent(get_indent(), TRUE);
  2035.  
  2036.             /* put cursor on last non-space */
  2037.             coladvance(MAXCOL);
  2038.             while (curwin->w_cursor.col && vim_isspace(gchar_cursor()))
  2039.                 dec_cursor();
  2040.             curs_columns(FALSE);            /* update curwin->w_virtcol */
  2041.  
  2042.             /* do the formatting */
  2043.             State = INSERT;        /* for Opencmd() */
  2044.             insertchar(NUL, TRUE, second_indent);
  2045.             State = NORMAL;
  2046.             first_par_line = TRUE;
  2047.             second_indent = -1;
  2048.         }
  2049.         else
  2050.         {
  2051.             /*
  2052.              * Still in same paragraph, so join the lines together.
  2053.              * But first delete the comment leader from the second line.
  2054.              */
  2055.             advance = FALSE;
  2056.             curwin->w_cursor.lnum++;
  2057.             curwin->w_cursor.col = 0;
  2058.             while (next_leader_len--)
  2059.                 delchar(FALSE);
  2060.             curwin->w_cursor.lnum--;
  2061.             if (do_join(TRUE, FALSE) == FAIL)
  2062.             {
  2063.                 beep_flush();
  2064.                 break;
  2065.             }
  2066.             first_par_line = FALSE;
  2067.         }
  2068.     }
  2069.     fo_do_comments = FALSE;
  2070.     /*
  2071.      * Leave the cursor at the first non-blank of the last formatted line.
  2072.      * If the cursor was move one line back (e.g. with "Q}") go to the next
  2073.      * line, so "." will do the next lines.
  2074.      */
  2075.     if (op_end_adjusted && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
  2076.         ++curwin->w_cursor.lnum;
  2077.     beginline(TRUE);
  2078.     updateScreen(NOT_VALID);
  2079.     msgmore(curbuf->b_ml.ml_line_count - old_line_count);
  2080. }
  2081.  
  2082. /*
  2083.  * Blank lines, and lines containing only the comment leader, are left
  2084.  * untouched by the formatting.  The function returns TRUE in this
  2085.  * case.  It also returns TRUE when a line starts with the end of a comment
  2086.  * ('e' in comment flags), so that this line is skipped, and not joined to the
  2087.  * previous line.  A new paragraph starts after a blank line, or when the
  2088.  * comment leader changes -- webb.
  2089.  */
  2090.     static int
  2091. fmt_end_block(lnum, leader_len, leader_flags)
  2092.     linenr_t    lnum;
  2093.     int            *leader_len;
  2094.     char_u        **leader_flags;
  2095. {
  2096.     char_u        *flags = NULL;        /* init for GCC */
  2097.     char_u        *ptr;
  2098.  
  2099.     ptr = ml_get(lnum);
  2100.     *leader_len = get_leader_len(ptr, leader_flags);
  2101.  
  2102.     if (*leader_len > 0)
  2103.     {
  2104.         /*
  2105.          * Search for 'e' flag in comment leader flags.
  2106.          */
  2107.         flags = *leader_flags;
  2108.         while (*flags && *flags != ':' && *flags != COM_END)
  2109.             ++flags;
  2110.     }
  2111.  
  2112.     return (ptr[*leader_len] == NUL ||
  2113.             (*leader_len > 0 && *flags == COM_END) ||
  2114.              startPS(lnum, NUL, FALSE));
  2115. }
  2116.  
  2117. /*
  2118.  * prepare a few things for block mode yank/delete/tilde
  2119.  *
  2120.  * for delete:
  2121.  * - textlen includes the first/last char to be (partly) deleted
  2122.  * - start/endspaces is the number of columns that are taken by the
  2123.  *     first/last deleted char minus the number of columns that have to be deleted.
  2124.  * for yank and tilde:
  2125.  * - textlen includes the first/last char to be wholly yanked
  2126.  * - start/endspaces is the number of columns of the first/last yanked char
  2127.  *   that are to be yanked.
  2128.  */
  2129.     static void
  2130. block_prep(bd, lnum, is_del)
  2131.     struct block_def    *bd;
  2132.     linenr_t            lnum;
  2133.     int                    is_del;
  2134. {
  2135.     colnr_t        vcol;
  2136.     int            incr = 0;
  2137.     char_u        *pend;
  2138.     char_u        *pstart;
  2139.  
  2140.     bd->startspaces = 0;
  2141.     bd->endspaces = 0;
  2142.     bd->textlen = 0;
  2143.     bd->textcol = 0;
  2144.     vcol = 0;
  2145.     pstart = ml_get(lnum);
  2146.     while (vcol < op_start_vcol && *pstart)
  2147.     {
  2148.         /* Count a tab for what it's worth (if list mode not on) */
  2149.         incr = lbr_chartabsize(pstart, (colnr_t)vcol);
  2150.         vcol += incr;
  2151.         ++pstart;
  2152.         ++bd->textcol;
  2153.     }
  2154.     if (vcol < op_start_vcol)    /* line too short */
  2155.     {
  2156.         if (!is_del)
  2157.             bd->endspaces = op_end_vcol - op_start_vcol + 1;
  2158.     }
  2159.     else /* vcol >= op_start_vcol */
  2160.     {
  2161.         bd->startspaces = vcol - op_start_vcol;
  2162.         if (is_del && vcol > op_start_vcol)
  2163.             bd->startspaces = incr - bd->startspaces;
  2164.         pend = pstart;
  2165.         if (vcol > op_end_vcol)        /* it's all in one character */
  2166.         {
  2167.             bd->startspaces = op_end_vcol - op_start_vcol + 1;
  2168.             if (is_del)
  2169.                 bd->startspaces = incr - bd->startspaces;
  2170.         }
  2171.         else
  2172.         {
  2173.             while (vcol <= op_end_vcol && *pend)
  2174.             {
  2175.                 /* Count a tab for what it's worth (if list mode not on) */
  2176.                 incr = lbr_chartabsize(pend, (colnr_t)vcol);
  2177.                 vcol += incr;
  2178.                 ++pend;
  2179.             }
  2180.             if (vcol < op_end_vcol && !is_del)    /* line too short */
  2181.             {
  2182.                 bd->endspaces = op_end_vcol - vcol;
  2183.             }
  2184.             else if (vcol > op_end_vcol)
  2185.             {
  2186.                 bd->endspaces = vcol - op_end_vcol - 1;
  2187.                 if (!is_del && pend != pstart && bd->endspaces)
  2188.                     --pend;
  2189.             }
  2190.         }
  2191.         if (is_del && bd->startspaces)
  2192.         {
  2193.             --pstart;
  2194.             --bd->textcol;
  2195.         }
  2196.         bd->textlen = (int)(pend - pstart);
  2197.     }
  2198.     bd->textstart = pstart;
  2199. }
  2200.  
  2201. #define NUMBUFLEN 30
  2202.  
  2203. /*
  2204.  * add or subtract 'Prenum1' from a number in a line
  2205.  * 'command' is CTRL-A for add, CTRL-X for subtract
  2206.  *
  2207.  * return FAIL for failure, OK otherwise
  2208.  */
  2209.     int
  2210. do_addsub(command, Prenum1)
  2211.     int            command;
  2212.     linenr_t    Prenum1;
  2213. {
  2214.     register int     col;
  2215.     char_u            buf[NUMBUFLEN];
  2216.     int                hex;            /* 'X': hexadecimal; '0': octal */
  2217.     static int        hexupper = FALSE;    /* 0xABC */
  2218.     long            n;
  2219.     char_u            *ptr;
  2220.     int                i;
  2221.     int                c;
  2222.     int                zeros = 0;        /* number of leading zeros */
  2223.     int                digits = 0;        /* number of digits in the number */
  2224.  
  2225.     ptr = ml_get_curline();
  2226.     col = curwin->w_cursor.col;
  2227.  
  2228.         /* first check if we are on a hexadecimal number */
  2229.     while (col > 0 && isxdigit(ptr[col]))
  2230.         --col;
  2231.     if (col > 0 && (ptr[col] == 'X' || ptr[col] == 'x') &&
  2232.                         ptr[col - 1] == '0' && isxdigit(ptr[col + 1]))
  2233.         --col;        /* found hexadecimal number */
  2234.     else
  2235.     {
  2236.         /* first search forward and then backward for start of number */
  2237.         col = curwin->w_cursor.col;
  2238.  
  2239.         while (ptr[col] != NUL && !isdigit(ptr[col]))
  2240.             ++col;
  2241.  
  2242.         while (col > 0 && isdigit(ptr[col - 1]))
  2243.             --col;
  2244.     }
  2245.  
  2246.     if (isdigit(ptr[col]) && u_save_cursor() == OK)
  2247.     {
  2248.         ptr = ml_get_curline();                    /* get it again, because
  2249.                                                    u_save may have changed it */
  2250.         curwin->w_set_curswant = TRUE;
  2251.  
  2252.         hex = 0;                                /* default is decimal */
  2253.         if (ptr[col] == '0')                    /* could be hex or octal */
  2254.         {
  2255.             hex = TO_UPPER(ptr[col + 1]);        /* assume hexadecimal */
  2256.             if (hex != 'X' || !isxdigit(ptr[col + 2]))
  2257.             {
  2258.                 if (isdigit(hex))
  2259.                     hex = '0';                    /* octal */
  2260.                 else
  2261.                     hex = 0;                    /* 0 by itself is decimal */
  2262.             }
  2263.         }
  2264.  
  2265.         if (!hex && col > 0 && ptr[col - 1] == '-')
  2266.             --col;
  2267.  
  2268.         ptr += col;
  2269.         /*
  2270.          * we copy the number into a buffer because some versions of sscanf
  2271.          * cannot handle characters with the upper bit set, making some special
  2272.          * characters handled like digits.
  2273.          */
  2274.         for (i = 0; *ptr && !(*ptr & 0x80) && i < NUMBUFLEN - 1; ++i)
  2275.             buf[i] = *ptr++;
  2276.         buf[i] = NUL;
  2277.  
  2278.         if (hex == '0')
  2279.             sscanf((char *)buf, "%lo", &n);
  2280.         else if (hex)
  2281.             sscanf((char *)buf + 2, "%lx", &n);    /* "%X" doesn't work! */
  2282.         else
  2283.             n = atol((char *)buf);
  2284.  
  2285.         if (command == Ctrl('A'))
  2286.             n += Prenum1;
  2287.         else
  2288.             n -= Prenum1;
  2289.  
  2290.         if (hex == 'X')                    /* skip the '0x' */
  2291.             col += 2;
  2292.         else if (hex == '0')
  2293.             col++;                        /* skip the '0' */
  2294.         curwin->w_cursor.col = col;
  2295.  
  2296.         c = gchar_cursor();
  2297.         do                                /* delete the old number */
  2298.         {
  2299.             if (digits == 0 && c == '0')
  2300.                 ++zeros;                /* count the number of leading zeros */
  2301.             else
  2302.                 ++digits;                /* count the number of digits */
  2303.             if (isalpha(c))
  2304.             {
  2305.                 if (isupper(c))
  2306.                     hexupper = TRUE;
  2307.                 else
  2308.                     hexupper = FALSE;
  2309.             }
  2310.             (void)delchar(FALSE);
  2311.             c = gchar_cursor();
  2312.         }
  2313.         while (hex ? (hex == '0' ? c >= '0' && c <= '7' :
  2314.                                         isxdigit(c)) : isdigit(c));
  2315.  
  2316.         if (hex == 0)
  2317.             sprintf((char *)buf, "%ld", n);
  2318.         else
  2319.         {
  2320.             if (hex == '0')
  2321.                 sprintf((char *)buf, "%lo", n);
  2322.             else if (hex && hexupper)
  2323.                 sprintf((char *)buf, "%lX", n);
  2324.             else if (hex)
  2325.                 sprintf((char *)buf, "%lx", n);
  2326.             /* adjust number of zeros to the new number of digits, so the
  2327.              * total length of the number remains the same */
  2328.             if (zeros)
  2329.             {
  2330.                 zeros += digits - STRLEN(buf);
  2331.                 if (zeros > 0)
  2332.                 {
  2333.                     vim_memmove(buf + zeros, buf, STRLEN(buf) + 1);
  2334.                     for (col = 0; zeros > 0; --zeros)
  2335.                         buf[col++] = '0';
  2336.                 }
  2337.             }
  2338.         }
  2339.         ins_str(buf);                    /* insert the new number */
  2340.         --curwin->w_cursor.col;
  2341.         updateline();
  2342.         return OK;
  2343.     }
  2344.     else
  2345.     {
  2346.         beep_flush();
  2347.         return FAIL;
  2348.     }
  2349. }
  2350.  
  2351. #ifdef VIMINFO
  2352.     int
  2353. read_viminfo_register(line, fp, force)
  2354.     char_u    *line;
  2355.     FILE    *fp;
  2356.     int        force;
  2357. {
  2358.     int        eof;
  2359.     int        do_it = TRUE;
  2360.     int        size;
  2361.     int        limit;
  2362.     int        i;
  2363.     int        set_prev = FALSE;
  2364.     char_u    *str;
  2365.     char_u    **array = NULL;
  2366.  
  2367.     /* We only get here (hopefully) if line[0] == '"' */
  2368.     str = line + 1;
  2369.     if (*str == '"')
  2370.     {
  2371.         set_prev = TRUE;
  2372.         str++;
  2373.     }
  2374.     if (!isalnum(*str) && *str != '-')
  2375.     {
  2376.         EMSG2("viminfo: Illegal register name in line %s", line);
  2377.         do_it = FALSE;
  2378.     }
  2379.     yankbuffer = *str++;
  2380.     get_yank_buffer(FALSE);
  2381.     yankbuffer = 0;
  2382.     if (!force && y_current->y_array != NULL)
  2383.         do_it = FALSE;
  2384.     size = 0;
  2385.     limit = 100;        /* Optimized for registers containing <= 100 lines */
  2386.     if (do_it)
  2387.     {
  2388.         if (set_prev)
  2389.             y_previous = y_current;
  2390.         vim_free(y_current->y_array);
  2391.         array = y_current->y_array =
  2392.                        (char_u **)alloc((unsigned)(limit * sizeof(char_u *)));
  2393.         str = skipwhite(str);
  2394.         if (STRNCMP(str, "CHAR", 4) == 0)
  2395.             y_current->y_type = MCHAR;
  2396.         else if (STRNCMP(str, "BLOCK", 5) == 0)
  2397.             y_current->y_type = MBLOCK;
  2398.         else
  2399.             y_current->y_type = MLINE;
  2400.     }
  2401.     while (!(eof = vim_fgets(line, LSIZE, fp)) && line[0] == TAB)
  2402.     {
  2403.         if (do_it)
  2404.         {
  2405.             if (size >= limit)
  2406.             {
  2407.                 y_current->y_array = (char_u **)
  2408.                               alloc((unsigned)(limit * 2 * sizeof(char_u *)));
  2409.                 for (i = 0; i < limit; i++)
  2410.                     y_current->y_array[i] = array[i];
  2411.                 vim_free(array);
  2412.                 limit *= 2;
  2413.                 array = y_current->y_array;
  2414.             }
  2415.             viminfo_readstring(line);
  2416.             str = strsave(line + 1);
  2417.             if (str != NULL)
  2418.                 array[size++] = str;
  2419.             else
  2420.                 do_it = FALSE;
  2421.         }
  2422.     }
  2423.     if (do_it)
  2424.     {
  2425.         if (size == 0)
  2426.         {
  2427.             vim_free(array);
  2428.             y_current->y_array = NULL;
  2429.         }
  2430.         else if (size < limit)
  2431.         {
  2432.             y_current->y_array =
  2433.                         (char_u **)alloc((unsigned)(size * sizeof(char_u *)));
  2434.             for (i = 0; i < size; i++)
  2435.                 y_current->y_array[i] = array[i];
  2436.             vim_free(array);
  2437.         }
  2438.         y_current->y_size = size;
  2439.     }
  2440.     return eof;
  2441. }
  2442.  
  2443.     void
  2444. write_viminfo_registers(fp)
  2445.     FILE    *fp;
  2446. {
  2447.     int        i, j;
  2448.     char_u    *type;
  2449.     char_u    c;
  2450.     int        num_lines;
  2451.     int        max_num_lines;
  2452.  
  2453.     fprintf(fp, "\n# Registers:\n");
  2454.  
  2455.     max_num_lines = get_viminfo_parameter('"');
  2456.     if (max_num_lines == 0)
  2457.         return;
  2458.     for (i = 0; i < NUM_REGISTERS; i++)
  2459.     {
  2460.         if (y_buf[i].y_array == NULL)
  2461.             continue;
  2462. #ifdef USE_GUI
  2463.         /* Skip '*' register, we don't want it back next time */
  2464.         if (i == GUI_SELECTION_REGISTER)
  2465.             continue;
  2466. #endif
  2467.         switch (y_buf[i].y_type)
  2468.         {
  2469.             case MLINE:
  2470.                 type = (char_u *)"LINE";
  2471.                 break;
  2472.             case MCHAR:
  2473.                 type = (char_u *)"CHAR";
  2474.                 break;
  2475.             case MBLOCK:
  2476.                 type = (char_u *)"BLOCK";
  2477.                 break;
  2478.             default:
  2479.                 sprintf((char *)IObuff, "Unknown register type %d",
  2480.                     y_buf[i].y_type);
  2481.                 emsg(IObuff);
  2482.                 type = (char_u *)"LINE";
  2483.                 break;
  2484.         }
  2485.         if (y_previous == &y_buf[i])
  2486.             fprintf(fp, "\"");
  2487.         if (i == DELETION_REGISTER)
  2488.             c = '-';
  2489.         else if (i < 10)
  2490.             c = '0' + i;
  2491.         else
  2492.             c = 'a' + i - 10;
  2493.         fprintf(fp, "\"%c\t%s\n", c, type);
  2494.         num_lines = y_buf[i].y_size;
  2495.  
  2496.         /* If max_num_lines < 0, then we save ALL the lines in the register */
  2497.         if (max_num_lines > 0 && num_lines > max_num_lines)
  2498.             num_lines = max_num_lines;
  2499.         for (j = 0; j < num_lines; j++)
  2500.         {
  2501.             putc('\t', fp);
  2502.             viminfo_writestring(fp, y_buf[i].y_array[j]);
  2503.         }
  2504.     }
  2505. }
  2506. #endif /* VIMINFO */
  2507.  
  2508. #if defined(USE_GUI) || defined(PROTO)
  2509. /*
  2510.  * Text selection stuff that uses the GUI selection register '*'.  When using a
  2511.  * GUI this may be text from another window, otherwise it is the last text we
  2512.  * had highlighted with VIsual mode.  With mouse support, clicking the middle
  2513.  * button performs the paste, otherwise you will need to do <"*p>.
  2514.  */
  2515.  
  2516.     void
  2517. gui_free_selection()
  2518. {
  2519.     struct yankbuf *y_ptr = y_current;
  2520.  
  2521.     y_current = &y_buf[GUI_SELECTION_REGISTER];        /* '*' register */
  2522.     free_yank_all();
  2523.     y_current->y_size = 0;
  2524.     y_current = y_ptr;
  2525. }
  2526.  
  2527. /*
  2528.  * Get the selected text and put it in the gui text register '*'.
  2529.  */
  2530.     void
  2531. gui_get_selection()
  2532. {
  2533.     struct yankbuf *old_y_previous, *old_y_current;
  2534.     char_u    old_yankbuffer;
  2535.     FPOS    old_cursor, old_visual;
  2536.     int        old_op_type;
  2537.  
  2538.     if (gui.selection.owned)
  2539.     {
  2540.         if (y_buf[GUI_SELECTION_REGISTER].y_array != NULL)
  2541.             return;
  2542.  
  2543.         /* Get the text between gui.selection.start & gui.selection.end */
  2544.         old_y_previous = y_previous;
  2545.         old_y_current = y_current;
  2546.         old_yankbuffer = yankbuffer;
  2547.         old_cursor = curwin->w_cursor;
  2548.         old_visual = VIsual;
  2549.         old_op_type = op_type;
  2550.         yankbuffer = '*';
  2551.         op_type = YANK;
  2552.         do_pending_operator('y', NUL, FALSE, NULL, NULL, 0, TRUE, TRUE);
  2553.         y_previous = old_y_previous;
  2554.         y_current = old_y_current;
  2555.         yankbuffer = old_yankbuffer;
  2556.         curwin->w_cursor = old_cursor;
  2557.         VIsual = old_visual;
  2558.         op_type = old_op_type;
  2559.     }
  2560.     else
  2561.     {
  2562.         gui_free_selection();
  2563.  
  2564.         /* Try to get selected text from another window */
  2565.         gui_request_selection();
  2566.     }
  2567. }
  2568.  
  2569. /* Convert from the GUI selection string into the '*' register */
  2570.     void
  2571. gui_yank_selection(type, str, len)
  2572.     int        type;
  2573.     char_u    *str;
  2574.     long_u    len;
  2575. {
  2576.     struct yankbuf *y_ptr = &y_buf[GUI_SELECTION_REGISTER];    /* '*' register */
  2577.     int        lnum;
  2578.     int        start;
  2579.     int        i;
  2580.  
  2581.     gui_free_selection();
  2582.  
  2583.     /* Count the number of lines within the string */
  2584.     y_ptr->y_size = 1;
  2585.     for (i = 0; i < len; i++)
  2586.         if (str[i] == '\n')
  2587.             y_ptr->y_size++;
  2588.  
  2589.     if (type != MCHAR && i > 0 && str[i - 1] == '\n')
  2590.         y_ptr->y_size--;
  2591.  
  2592.     y_ptr->y_array = (char_u **)lalloc(y_ptr->y_size * sizeof(char_u *), TRUE);
  2593.     if (y_ptr->y_array == NULL)
  2594.         return;
  2595.     y_ptr->y_type = type;
  2596.     lnum = 0;
  2597.     start = 0;
  2598.     for (i = 0; i < len; i++)
  2599.     {
  2600.         if (str[i] == NUL)
  2601.             str[i] = '\n';
  2602.         else if (str[i] == '\n')
  2603.         {
  2604.             str[i] = NUL;
  2605.             if (type == MCHAR || i != len - 1)
  2606.             {
  2607.                 if ((y_ptr->y_array[lnum] = strsave(str + start)) == NULL)
  2608.                 {
  2609.                     y_ptr->y_size = lnum;
  2610.                     return;
  2611.                 }
  2612.                 lnum++;
  2613.                 start = i + 1;
  2614.             }
  2615.         }
  2616.     }
  2617.     if ((y_ptr->y_array[lnum] = alloc(i - start + 1)) == NULL)
  2618.         return;
  2619.     if (i - start > 0)
  2620.         STRNCPY(y_ptr->y_array[lnum], str + start, i - start);
  2621.     y_ptr->y_array[lnum][i - start] = NUL;
  2622.     y_ptr->y_size = lnum + 1;
  2623. }
  2624.  
  2625. /*
  2626.  * Convert the '*' register into a GUI selection string returned in *str with
  2627.  * length *len.
  2628.  */
  2629.     int
  2630. gui_convert_selection(str, len)
  2631.     char_u    **str;
  2632.     long_u    *len;
  2633. {
  2634.     struct yankbuf *y_ptr = &y_buf[GUI_SELECTION_REGISTER];    /* '*' register */
  2635.     char_u    *p;
  2636.     int        lnum;
  2637.     int        i, j;
  2638.  
  2639.     *str = NULL;
  2640.     *len = 0;
  2641.     if (y_ptr->y_array == NULL)
  2642.         return -1;
  2643.  
  2644.     for (i = 0; i < y_ptr->y_size; i++)
  2645.         *len += STRLEN(y_ptr->y_array[i]) + 1;
  2646.  
  2647.     /*
  2648.      * Don't want newline character at end of last line if we're in MCHAR mode.
  2649.      */
  2650.     if (y_ptr->y_type == MCHAR && *len > 1)
  2651.         (*len)--;
  2652.  
  2653.     p = *str = lalloc(*len, TRUE);
  2654.     if (p == NULL)
  2655.         return -1;
  2656.     lnum = 0;
  2657.     for (i = 0, j = 0; i < *len; i++, j++)
  2658.     {
  2659.         if (y_ptr->y_array[lnum][j] == '\n')
  2660.             p[i] = NUL;
  2661.         else if (y_ptr->y_array[lnum][j] == NUL)
  2662.         {
  2663.             p[i] = '\n';
  2664.             lnum++;
  2665.             j = -1;
  2666.         }
  2667.         else
  2668.             p[i] = y_ptr->y_array[lnum][j];
  2669.     }
  2670.     return y_ptr->y_type;
  2671. }
  2672. #endif /* USE_GUI || PROTO */
  2673.